﻿using Application.Abstractions.Messaging;
using Application.Todos.Get;
using Mediator;
using SharedKernel;
using WebApi.Extensions;
using WebApi.Infrastructure;

namespace WebApi.Endpoints.Todos;

internal sealed class Get : IEndpoint
{
    public void MapEndpoint(IEndpointRouteBuilder app)
    {
        app.MapGet("todos", async (
            IMediator mediator,
            CancellationToken cancellationToken) =>
        {
            var query = new GetTodosQuery();
            Result<PaginatedList<TodoResponse>> result = await mediator.Send(query, cancellationToken);
            return result.Match(Results.Ok, ApiResponse.Problem);
        })
        .WithTags(Tags.Todos)
        .HasApiVersion(1, 0)
        .RequireAuthorization();
    }
}
